package com.share.common.tools.fille;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * base64 文件工具类
 */
public class Base64Util {

    /**
     * SpringMvc 文件转base64
     *
     * @param file 图片文件
     * @return base64加密字符串
     */
    public static String fileToBase64(MultipartFile file) {
        String imgData = null;
        try {
            imgData = Base64.encode(file.getBytes());
        } catch (IOException e) {

            e.printStackTrace();
        }
        if (StringUtils.isEmpty(imgData)) {
            return "error";
        }
        return "data:image/png;base64,"+imgData;
    }


    public static String fileToBase64(File file) {
        String imgData = null;
        byte[] buffer;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
            imgData = Base64.encode(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(imgData)) {
            return "error";
        }
        return "data:image/png;base64,"+imgData;
    }


    public static String getImageStr( final String imgFile ) {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理

        InputStream in ;
        byte[] data ;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);


            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e) {
            return null;

        }
        //对字节数组Base64编码
        if (!StringUtils.isEmpty(data)){
        return "data:image/png;base64,"+Base64.encode(data);//返回Base64编码过的字节数组字符串

        }
        return null;
    }
    /**
     * 将base64编码字符串转换为图片
     *
     * @param imgStr base64编码字符串
     * @param path   图片路径-具体到文件
     * @return 是否成功
     */

    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        try {
            // 解密
            byte[] b = Base64.decode(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据图片地址转换为base64编码字符串
     *
     * @param imgFile 图片地址
     * @return 转换后的编码
     */
/*    public static String getImageStr(String imgFile) {
        InputStream inputStream;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }*/

    /**
     * byte 转base
     * @param data 待存贮的数据
     * @return 返回的数据
     */
    public static String dateToBase64(byte[] data){
        if (data.length==0){
            return null;
        }
        String imgData;
        imgData = Base64.encode(data);
        if (StringUtils.isEmpty(imgData)){
            return null;
        }
        return "data:image/png;base64,"+imgData;
    }

}
